<?php

namespace Rblog\Module\Index\Model;

class Comment extends \Rexzy\App\Model {

    protected $_table = 'comment';
    
    
    /**
     * 添加新评论
     * 
     * @param array() $val 评论信息
     * @return boolean 添加是否成功
     */
    public function add( $val ) {
        $value = array(
            'name' => $val['name'],
            'email' => $val['email'],
            'parent' => $val['parent'],
            'children' => 0,
            'artid' => $val['artid'],
            'date' => time(),
            'content' => $val['commentval'],
        );
        
        if ( $val['parent'] > 0 ) {
            $this->update( array( 'children' => 1 ), array( 'commentid' => $val['parent'] ) );
        }
        
        $result = $this->insert( $value );
        
        if ( $result > 0 ) {
            return true;
        }
        return false;
    }
    
    /**
     *  查询某篇文章的全部评论
     * 
     * @param int $artid 文章id
     * @return array() 查询结果
     */
    public function get( $artid ) {
        
        function getcomment( $artid, $parent, $data ) {
            $comments = array();
            $where = array(
                'artid' => $artid,
                'parent' => $parent,
            );
            $result = $data->selectAll( '*', $where );
            foreach ( $result as $key => $val ) {
                $result[$key]['date'] = date( 'Y-m-d', $val['date'] );
                if ( $val['children'] > 0 ) {
                    $result[$key]['children'] = array();
                    $children = getcomment( $artid, $val['commentid'], $data );
                    $result[$key]['children'] = $children;
                }
                $comments[$key] = $result[$key];
            }
            
            return $comments;
        }
        $comments = getcomment( $artid, 0, $this );
        return $comments;
    }
    
    /**
     * 查询某篇文章的评论总数
     * 
     * @param int $artid 文章id
     * @return int 
     */
    public function gettotal( $artid ) {
        $result = $this->selectAll( 'commentid', array( 'artid' => $artid ) );
        return count( $result );
    }
    
    /**
     * 查询一组文章里每篇文章的评论量
     * 
     * @param array() $arts 包含要查询的文章的信息的数组
     * @return array()
     */
    public function getEACNum( $arts ) {
        foreach ( $arts as $key => $val ) {
            $where = array( 'artid' => $val['artid'] );
            $arts[$key]['comment'] = $this->select( 'count(*)', $where )['count(*)'];
        }
        
        return $arts;
    }
    
    /**
     * 查询当前最新的10条评论
     * 
     * @return array()
     */
    public function getNewest() {
        $where = "group by commentid order by commentid desc";
        $comments = $this->selectAll( 'name, artid, content', $where, 10 );
        $artData = \Rexzy::model( 'Art' );
        foreach( $comments as $key => $val ) {
            $arttitle = $artData->select( 'title', array( 'artid' => $val['artid'] ) )['title'];
            $comments[$key]['artTitle'] = $arttitle;
        }
        
        return $comments;
    }
}
